home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / win_a_d / bmpclp.zip / BMPCLIP.TXT < prev   
Text File  |  1992-02-05  |  7KB  |  136 lines

  1. 1/30/91
  2.  
  3.     BMPCLIP is an adaptation of the program BSCRLAPP.PAS, a demo
  4.     program provided with Turbo Pascal for Windows(TPW).  I modified
  5.     it to add the ability to copy the visible area of the bitmap to the Win3
  6.     clipboard. Another member of the CIS TPW forum added the capability
  7.     to display 256 color bitmaps.  As a bitmap viewer, it is rather limited
  8.     in features, but the source code is a good example of bitmap handling
  9.     in TPW.  Of course, you need TPW to compile the code.  
  10.  
  11.     Files:
  12.  
  13.         BMPCLIP.EXE -compiled bitmap viewer
  14.         BMPCLIP.PAS -TPW source code
  15.         BMPCLIP.RES -Win3 resource file
  16.         BMPCLIP.TXT -this text file
  17.  
  18.     If you are intrested in the code, here is the documentation I
  19.     uploaded to the CIS TPW forum describing the copy method
  20.     I added to the demo program:
  21.  
  22.  
  23.     A few notes on the CMCopyBmp procedure........
  24.  
  25.  
  26.       This is a simple procedure that allows the user of a program that 
  27.     displays bitmaps to copy the entire visible area of the currently active
  28.     window to the clipboard. I have found this procedure to work reliably
  29.     with monochrome(2 color), VGA(16 color) and SVGA(256 color) bitmaps.
  30.     Most  Windows programs use rectangular banding, where the user
  31.     presses and holds the left mouse button to drag a rectangle over the
  32.     area to be copied, releases the button and chooses the Copy command 
  33.     from the Edit menu to paste the area to the clipboard. I was working
  34.     on an MDI Bitmap\Text viewer(DTPBrowser, functional demo available
  35.     free on Microsoft Windows Advanced Users Forum, full function
  36.     version $29) and found this method of cutting and pasting to be awkward
  37.     when working with several small bitmap windows-you had to do a
  38.     lot of scrolling to get the area you wanted, then go through the whole
  39.     banding procedure, open the edit menu and choose copy, etc. It seemed
  40.     a lot simpler to just resize the window to show the desired area and 
  41.     choose copy. Since this procedure returns a value of type TMessage,
  42.                 it is also a simple matter to add code to copy the bitmap window 
  43.     simply by pressing a hotkey or even a mouse button, eliminating the
  44.     need to open the Edit menu.  You should be able to use this procedure 
  45.                 with little or no modification in your programs.
  46.  
  47.     The code for this procedure is quite simple...
  48.                 There are a few preliminary statements that enable the main procedure:
  49.  
  50.     const                    -->declare return value for Copy command added to
  51.                   cm_Copy = 205;     to File menu (see resource file BMPCLIP.RES)
  52.  
  53.      procedure CMCopyBmp(var Msg: TMessage); virtual cm_First + cm_Copy;      
  54.                                              ----> method declaration for Copy procedure
  55.  
  56.     EnableMenuItem(Attr.Menu, cm_Copy, mf_ByCommand or mf_Grayed); 
  57.                                               ----> disables Copy command on menu when no bitmap is loaded
  58.  
  59.     EnableMenuItem(Attr.Menu, cm_Copy, mf_ByCommand or mf_Enabled);
  60.                                               ----->enables Copy command after bitmap is loaded
  61.  
  62.                 And in the Copy procedure itself....... 
  63.  
  64.     DC := GetDC(HWindow); gets the device context of the active window 
  65.     
  66.                MemDC1 := CreateCompatibleDC(DC); opens a compatible memory DC
  67.      for our new bitmap
  68.  
  69.                GetClientRect(HWindow, R);              These three lines copy the coordinates
  70.                NWidth := R.Right;                ----------> of the active window to record R of type
  71.                NHeight := R.Bottom;                         TRect from which we get the integers
  72.                                                                Right and Bottom that are the width 
  73.                                                                and height of the active window.
  74.  
  75.                NewBmp := CreateCompatibleBitmap(DC, NWidth, NHeight); ---->creates bitmap
  76.                OldBitmap1 := SelectObject(MemDC1, NewBmp);                      handle NewBmp
  77.                                                                                                            and selects  our DC into it.
  78.  
  79.                if NewBmp = 0 then                                                          If there is not enough 
  80.                begin                                                                         ----->memory for the operation, 
  81.                   MessageBox(HWindow, 'Unable to copy Bitmap',      display error message,
  82.                       'Error', mb_IconExclamation or mb_ok);                  get rid of associated data 
  83.       SelectObject(MemDC1, OldBitmap1);                           structures and terminate
  84.                   DeleteDC(MemDC1);                                                     procedure.
  85.                   ReleaseDC(HWindow,DC)
  86.                end
  87.  
  88.               else begin
  89.                  OldCursor := SetCursor(LoadCursor(0, idc_Wait));   --> display hourglass cursor
  90.  
  91.      BitBlt(MemDC1, 0, 0, NWidth, NHeight, DC, 0, 0,  -->  copy pixel bits from current
  92.          Mode);                                                                       DC(DC) to new DC(MemDC1)
  93.  
  94.     OpenClipboard(HWindow);                                         Opens and clears clipboard,
  95.                 EmptyClipboard;                                        --------->      set data type to cf_Bitmap, get
  96.                 SetClipboardData(cf_Bitmap, NewBmp);                    bitmap handle, close clipboard.
  97.     CloseClipboard;
  98.  
  99.     SetCursor(OldCursor);     ---->    Reset cursor to whatever it was before  
  100.                 
  101.     SelectObject(MemDC1, OldBitmap1);
  102.                 DeleteDC(MemDC1);                          -------> clean up the mess
  103.                 ReleaseDC(HWindow,DC)
  104.  
  105.     IMPORTANT: If you want to put in error handling routines in between
  106.     the OpenClipboard and CloseClipboard statements, make sure they do not
  107.     terminate the procedure before closing the clipboard, as this will disable it
  108.     for the remainder of your windows session(I have run across one or two
  109.     shareware image viewers that do this).  
  110.  
  111.  
  112.     Anyway, I know this procedure is crude, but it works in both standard and 
  113.     MDI applications. (If you are using it in an MDI app, be sure to call it from
  114.     the child window you use to display bitmaps-The TDW manual says menu 
  115.                 procedures in MDI apps must be called from the MDI "Frame" window, but
  116.     the procedure won't work this way.)  The industrious ones among you might
  117.     add more sophisticated error checking and handling, or even banding(ugh!).
  118.     I uploaded this as is to help those using TPW and ObjectWindows to write
  119.                 graphics applications because we often run into problems due to OWL's lack 
  120.                 of advanced graphics handling methods, so maybe this will help a few of you.
  121.     It goes without saying that those of you with more advanced knowledge of
  122.     TPW programming(which is probably most of you) could find better ways
  123.     to do this, and I would be quite intrested to see them.
  124.  
  125.                 The sources for this code were two superb books on Windows programming:
  126.     "Programming Windows" (2nd Ed.) by Charles Petzold and     "Turbo Pascal for
  127.     Windows 3.0 Programming" by Tom Swan. I also found the article "DDE and
  128.     the Clipboard in Windows" by Ray Duncan in his Power Programming column
  129.     (PC Magazine 5/14/91) to be quite helpful.
  130.  
  131.     Enjoy,
  132.  
  133.        Scott Hanrahan [70144, 3033]
  134.  
  135.  
  136.